package com.example.config.es.template.impl;

import com.example.config.es.template.SyncStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.stereotype.Component;

// 最终一致性策略（异步执行）
@Component("finalConsistency")
public class FinalConsistencyStrategy<T,ID> implements SyncStrategy<T,ID> {
    @Autowired
    private ElasticsearchOperations esOperations;
    @Autowired
    private AsyncTaskExecutor asyncExecutor;

    @Override
    public <T> void syncSaveOrUpdate(T entity) {
        asyncExecutor.execute(() -> esOperations.save(entity)); // 异步保存
    }

    @Override
    public <T> void syncDelete(Class<T> entityClass, ID id) {
        asyncExecutor.execute(() -> {
            esOperations.delete(id.toString(),entityClass);
        }); // 异步删除
    }
}
